//
// Created by steven on 2017/3/9.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMOrderDataController.h"
#import "GMComboRecord.h"
#import "GMComboModel.h"
#import "GMOrderRecord.h"


@implementation GMOrderDataController

- (void)requestCombosWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;

    [self requestWithType:GET url:ACTION_COMBO params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *metaInfo = object[@"meta"][@"pagination"];
        NSArray *data = object[@"data"];
        strongSelf.comboRecord.totalCount = [metaInfo[@"total"] integerValue];
        strongSelf.comboRecord.currentPage = [metaInfo[@"current_page"] integerValue];
        strongSelf.comboRecord.totalPage = [metaInfo[@"total_pages"] integerValue];

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            GMComboModel *comboModel = [[GMComboModel alloc] init];
            comboModel.id = [obj[@"id"] integerValue];
            comboModel.name = obj[@"name"];
            comboModel.quantity = obj[@"quantity"];
            comboModel.price = [obj[@"price"] stringValue];
            comboModel.maxDiscount = [obj[@"maxDiscount"] stringValue];
            comboModel.score = [obj[@"score"] stringValue];

            [tempArray addObject:comboModel];
        }];

        if (strongSelf.comboRecord.currentPage == 1) {
            [strongSelf.comboRecord.combos removeAllObjects];
        }

        [strongSelf.comboRecord.combos addObjectsFromArray:tempArray];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取订单失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)requestOrdersWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;

    [self requestWithType:GET url:ACTION_ORDER params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *metaInfo = object[@"meta"][@"pagination"];
        NSArray *data = object[@"data"];
        strongSelf.orderRecord.totalCount = [metaInfo[@"total"] integerValue];
        strongSelf.orderRecord.currentPage = [metaInfo[@"current_page"] integerValue];
        strongSelf.orderRecord.totalPage = [metaInfo[@"total_pages"] integerValue];

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            GMOrderModel *orderModel = [[GMOrderModel alloc] init];
            orderModel.id = [obj[@"id"] integerValue];
            orderModel.name = obj[@"name"];
            orderModel.price = [obj[@"price"] stringValue];
            orderModel.fee = [obj[@"fee"] stringValue];
            orderModel.type = obj[@"apple"];
            orderModel.outTradeNo = obj[@"outTradeNo"];
            orderModel.status = obj[@"status"];
            orderModel.buyTime = obj[@"buyTime"];
            orderModel.count = [obj[@"quantity"] stringValue];

            [tempArray addObject:orderModel];
        }];

        if (strongSelf.orderRecord.currentPage == 1) {
            [strongSelf.orderRecord.orders removeAllObjects];
        }

        [strongSelf.orderRecord.orders addObjectsFromArray:tempArray];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取订单失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)buyComboWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_BUY params:params success:^(id object) {
        NSString *id = [object[@"data"][@"id"] stringValue];
        NSDictionary *verifyParams = @{@"outTradeNo": id};
        [self verifyWithParams:verifyParams callback:^(NSString *error) {
            if (error == nil) {
                callback(nil);
            } else {
                callback(error);
            }
        }];

    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"购买失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)verifyWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_APPLE_PAY params:params success:^(id object) {
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"购买失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (GMComboRecord *)comboRecord {
    if (!_comboRecord) {
        _comboRecord = [[GMComboRecord alloc] init];
    }

    return _comboRecord;
}

- (GMOrderRecord *)orderRecord {
    if (!_orderRecord) {
        _orderRecord = [[GMOrderRecord alloc] init];
    }

    return _orderRecord;
}

@end
